package com.ccsu.mymarket.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ccsu.mymarket.entity.Shop;
import com.ccsu.mymarket.entity.message.Meta;
import com.ccsu.mymarket.entity.message.Result;
import com.ccsu.mymarket.jwt.UserLoginToken;
import com.ccsu.mymarket.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@UserLoginToken
@RestController
@RequestMapping("/shop")
public class ShopController {
    private ShopService shopService;

    public ShopController(@Autowired ShopService shopService) {
        this.shopService = shopService;
    }

    @GetMapping("/page/{pagenum}/{pagesize}")
    @UserLoginToken
//    给管理员用的，查看所有店铺信息,分页
    public Result getShop(@PathVariable Integer pagenum, @PathVariable Integer pagesize) {
        List<Shop> shopList = shopService.getShopForAdmin(pagenum, pagesize);
        if (shopList == null)
            return new Result(new Meta(201, "暂无数据"), shopList);
        return new Result(new Meta(200, "查询成功！"), shopList);
    }

    @GetMapping("/search/seller/{id}")
    public Result searchShopForSeller(@PathVariable Integer id){
        Shop shop=shopService.getById(id);
        if (shop.getId() != null)
            return new Result(new Meta(200, "查询成功！"), shop);
        return new Result(new Meta(201, "暂无数据"), null);
    }

    @GetMapping("/search/admin")
//    传入店铺名关键字key + 起始条数pagenum + 限制长度pagesize
    public Result searchShopForAdmin(@RequestBody Map data) {
        String key = (String) data.get("key");
        Integer pagenum = (Integer) data.get("pagenum");
        Integer pagesize = (Integer) data.get("pagesize");

        List<Shop> shopList = shopService.getShopForAdmin(pagenum, pagesize).stream()
                .filter(shop -> shop.getShopName().contains(key))
                .collect(Collectors.toList());
        if (shopList != null)
            return new Result(new Meta(201, "暂无数据"), shopList);
        return new Result(new Meta(201, "暂无数据"), null);
    }

    @DeleteMapping("/delete/{id}")
//    传入店铺id
    public Result deleteShop(@PathVariable Integer id) {
        if (shopService.deleteShop(id))
            return new Result(new Meta(200, "操作成功！"), null);
        return new Result(new Meta(500, "发生错误！"), null);
    }

    @PutMapping("/update")
//    根据Shop实体类传入对象，除店铺id外字段可选
    public Result updateShop(@RequestBody Shop shop) {
        if(shop == null || shop.getId()==null)
            return new Result(new Meta(403, "请输入数据！"), null);
        if (shopService.update(new LambdaUpdateWrapper<Shop>()
                                .eq(Shop::getId, shop.getId())
                                .set(shop.getLogImg() != null, Shop::getLogImg, shop.getLogImg())
                                .set(shop.getShopName() != null, Shop::getShopName, shop.getShopName())
                                .set(shop.getType() != null, Shop::getType, shop.getType())
                                .set(shop.getState() != null, Shop::getState, shop.getState()))
        ){
            return new Result(new Meta(200, "操作成功！"), null);
        }
        return new Result(new Meta(500, "发生错误！"), null);
    }

}
